#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h> 
#include <errno.h>

//struct hostent {
//    char  *h_name;            /* official name of host */
//    char **h_aliases;         /* alias list */
//    int    h_addrtype;        /* host address type */
//    int    h_length;          /* length of address */
//    char **h_addr_list;       /* list of addresses */
//};
//#define h_addr h_addr_list[0] /* for backward compatibility */

void error(char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    struct hostent      *server;
    struct in_addr      **addr_list;
    char                **alia_list;

    if (argc < 2) {
       fprintf(stderr,"usage %s hostname\n", argv[0]);
       exit(0);
    }
    if ((server = gethostbyname(argv[1])) == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }

    printf("hostent:\n");
    printf("\th_name:%s\n", server->h_name);
    alia_list = (char **)server->h_aliases;
    while(*alia_list != NULL) 
        printf("\th_aliases:%s\n", *alia_list++);
    printf("\th_addrtype:%d\n", server->h_addrtype);
    printf("\th_length:%d\n", server->h_length);
    addr_list = (struct in_addr **)server->h_addr_list;
    while(*addr_list != NULL) {
        //注意一定要包括<arpa/inet.h>头文件，不然
        //可能导入的就是另一个函数
        printf("\th_addr_list:%s\n", inet_ntoa(**addr_list++));
    }


    return 0;
}
